<?php
namespace app\admin\controller;

use app\admin\model\ImageLibrary as ImageLibraryModel;
use app\admin\model\Device as DeviceModel;
use think\Db;
use think\Request;

class ImageLibrary extends Base
{
    public function index()
    {
        try {
            $model = new ImageLibraryModel;
            $dModel = new DeviceModel;

            //搜索名称
            $phname = $this->request->param("phname");

            //渲染页面模板
            $model->field('i.id,i.title,i.if_url,i.url,i.created_at,i.updated_at,i.illustration,d.name,s.sn')
                ->alias('i')
                ->join('device d','i.device_id = d.id','left')
                ->join('sn s','d.sn_id = s.id','left')
                ->order('i.id','desc');
            //  ->select();
            //  ->paginate(5);

            if(!empty($phname)){
                $model->where('i.title','like', '%'.$phname.'%');
            }
            $result = $model->paginate(10,false,[
                'query'=>['phname'=>$phname]
            ]);

            //渲染用户组
            $device = $dModel->field('id,sn,name')->select();

            //计算总共记录条数
            $count = $result->total();
            //分页
            $page = $result->render();

            //接收ajax传递的值
            $request = request();
            $ispost = $request->isPost();
            if($ispost){
                $id = $_POST["viewId"];
                $view = $model->getAll()->where("id",$id)->select();
                return json([
                    'code' => 200,
                    'view'=>$view,
                ]);
            }


            $this->assign('currentPage',$result->currentPage());
            $this->assign('lastPage',$result->lastPage());
            $this->assign('page',$page);
            $this->assign('device',$device);
            $this->assign('image',$result);
            $this->assign('count',$count);
            $this->assign('empty',"暂无数据");
            return $this->fetch();
        } catch (\Exception $e) {
            return json(['errcode'=>4000, 'message'=>$e->getMessage()]);
        }
    }


    //删除单条记录
    public function delete($id)
    {
        try {
            $result = ImageLibraryModel::update(['deleted_at'=>date('Y-m-d H:i:s')],['id'=>$id]);//软删除
            if($result){
                return json([
                    'code' => 200,
                ]);
            }
        } catch (\Exception $e) {
        return json(['errcode'=>4000, 'message'=>$e->getMessage()]);
        }

    }

    //批量删除
    public function deleteArr()
    {
        $ispost = $this->request->isPost();
        if ($ispost) {
            $idArr = $this->request->post();

            $result = ImageLibraryModel::where('id','exp','in('.implode(',',$idArr['id']).')')->update(['deleted_at'=>date('Y-m-d H:i:s')]);//软删除

            if($result){
                return json([
                    'code' => 200,
                ]);
            }

        }
    }

    //下载
    public function download($url)
    {
        $file_dir = ROOT_PATH . 'public'  . $url;    // 下载文件存放目录

        // 检查文件是否存在
        if (! file_exists($file_dir) ) {
            $this->error('文件未找到');
        }else {
            try{
                // 打开文件
                $file1 = fopen($file_dir, "r");
                $fileSize = filesize($file_dir);
                // 输入文件标签
                Header("Content-type: application/octet-stream");
                Header("Accept-Ranges: bytes");
                Header("Accept-Length:" . $fileSize);
                Header("Content-Disposition: attachment;filename=" . basename($file_dir));
                ob_clean();     // 重点！！！
                flush();        // 重点！！！！可以清除文件中多余的路径名以及解决乱码的问题：
                //输出文件内容
                //读取文件内容并直接输出到浏览器
                echo fread($file1, $fileSize);
                fclose($file1);
                exit();
            }catch (\Exception $e){
                return json([
                    'code' => 4000,
                    'message'=>$e->getMessage()
                ]);
            }
        }
    }
}